<template>
	<div class="fix_a">
		<van-nav-bar
		  class="fix_header"	
		  title="管理收货地址"
		  left-arrow
		  @click-left="onClickLeft"
		>		
		  <van-icon name="arrow-left" slot="left" color="#000" size="20"/>
	    </van-nav-bar>	
	    <div class="fix_next"></div>
         
	    <div >
	    	<van-list
			  v-model="loading"
			  :finished="finished"
			  finished-text="没有更多了"
			  @load="onLoad"
			  :offset="150"
			  :immediate-check='false'
            >
				<van-address-list
				  v-model="chosenAddressId"
				  :list="list"		 			  
				  @add="onAdd"
				  @edit="onEdit"
				  default-tag-text="默认"
				  @select='select'
				/>
			</van-list>
	    </div> 	
	    	    
	</div>	
</template>

<script>
import {getFun} from '@/api/publicFun.js'	 
let publicFun=getFun()	
export default {	
	data() {
       return {
       	  comId:'', // 绑定商品ID（点击的时候回到指定的地址）
       	  loading:false,
       	  finished:false,
       	  headerParams:{
       	  	pageSize:10,
       	  	pageNum:1
       	  },
       	  params:{
       	  	memberId:''
       	  },
       	  url1:'/shop/address/list',
       	  url2:'/shop/address/edit',
	      chosenAddressId: '1',
	      list: []     
       }
	},
	methods: {	
       onAdd(){
       	 let arg='[{"comId":"'+this.comId+'"}]' 
       	 this.link('/market/order/buy/add',arg)
       },
       onEdit(item){
       	 let arg='[{"addressId":"'+item.addressId+'"},{"comId":"'+this.comId+'"}]' 
         this.link('/market/order/buy/edit',arg)
       },
       // 切换默认地址 (无感知)
       select(item,index){   
       	 let params={
       	 	"memberId":this.params.memberId,
       	 	"addressId":item.addressId,
       	 	"addressIsDefault":1
       	 }
       	 let that=this
		 this.$Axios.Post(this.url2,params)
		 .then(function(res) {
		    let arg='[{"userId":"'+that.params.memberId+'"},{"comId":"'+that.comId+'"}]' 
            that.link('/market/order/buy',arg)            
		 })       	        	 
       },
       getData(){
      	let that=this	      	
	    this.$Axios.postRequest(this.url1,this.params,this.headerParams)
	    .then(function(res) {
	      if(res.data.page.current>=res.data.page.pages){
	      	that.finished=true
	      }
	      that.loading=false	
          let arr=res.data.list
          for(var i=0;i<arr.length;i++){
          	arr[i].id=arr[i].addressId
          	arr[i].name=arr[i].addressRealName
          	arr[i].tel=arr[i].addressMobile
          	arr[i].address=arr[i].addressAreaInfo+arr[i].address
          	arr[i].isDefault=arr[i].addressIsDefault>0?true:false
          	if(arr[i].addressIsDefault>0){
          		that.chosenAddressId=arr[i].addressId
          	}
          }
          that.list=that.list.concat(arr)
	    })	       	 
       },
       onLoad(){
         this.headerParams.pageNum=this.headerParams.pageNum+1
         this.getData()
       }
	},
	mixins:[publicFun],//混入		
	mounted() {
		this.params.memberId=this.$router.currentRoute.query.userId
		this.comId=this.$router.currentRoute.query.comId
		this.getData()
	}
}	
</script>

<style scoped="">
.margin10{margin: 10px 0;}
</style>